<?php namespace App\Http\Controllers;

use App\AboutUs;
use App\Admin;
use App\ApplicationField;
use App\Banner;
use App\BooksChapter;
use App\BooksInfo;
use App\Classify;
use App\EnAboutUs;
use App\EnApplicationField;
use App\EnBanner;
use App\EnFooterMenu;
use App\EnGoodsProduct;
use App\EnHumanResources;
use App\EnNews;
use App\EnSysInfo;
use App\FooterMenu;
use App\GoodsProduct;
use App\Http\Controllers\Api\AudioController;
use App\Http\Services\FileManager;
use App\HumanResources;
use App\News;
use App\SysInfo;
use App\UserVip;
use App\VipRule;
use Blocktrail\CryptoJSAES\CryptoJSAES;
use App\RechargePackage;
use App\User;
use App\UserInfo;
use App\UserSpendRecord;
use Illuminate\Contracts\Auth\Guard;
use DB;
use Auth;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\File;

class AdminController extends Controller
{
    public function login()
    {
        return view('admin.login');
    }

    public function bannerList()
    {
        if (Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $type = 'banner';

        /** @var Banner $sysBanner */
        $sysBanners = Banner::orderBy('sort', 'asc')->orderBy('created_at', 'desc')->get();
        if (count($sysBanners) > 0) {
            foreach ($sysBanners as $sysBanner) {
                $sysBanner->img_url = FileManager::getImgUrl($sysBanner->img_id);
            }
        }

        return view('admin.banner-list', ['banners' => $sysBanners,'type' => $type]);
    }

    public function bannerSet(Request $request)
    {
        if (Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $type = 'banner';

        $id = !is_null($request->get('id')) ? $request->get('id') : 0;

        /** @var Banner $thatBanner */
        $thatBanner = Banner::whereId($id)->first();
        if ($thatBanner) {
            $thatBanner->img_url = FileManager::getImgUrl($thatBanner->img_id);
        }

        return view('admin.banner-set', ['banner' => $thatBanner,'type' => $type]);
    }

    public function userList(Request $request)
    {
        if (Auth::guard("admin")->guest())
            return response()->json(['level' => 'error', 'message' => '请先登录！']);

        $users = User::orderBy("created_at", "desc")->paginate(50);

        foreach ($users as $user) {
            /** @var User $user */
            $user->userInfo;
            $user->userInfo->photo_url = FileManager::getImgUrl($user->userInfo->photo_id);
        }

        return view('admin.user-list', ["users" => $users]);
    }

    public function adminList(Request $request)
    {
        if (Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        /** @var Admin $newAdmin */
        $newAdmins = Admin::orderBy('created_at', 'desc')->paginate(10);

        return view('admin.admin-list', ['admins' => $newAdmins]);
    }

    public function adminSet(Request $request)
    {
        if (Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        return view('admin.admin-set');
    }

    public function changePwd()
    {
        if (Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        return view('admin.change-psw');
    }

    public function classifyList(Request $request)
    {
        if (Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $classify = Classify::orderBy('sort', 'asc')->orderBy('created_at', 'desc')->get();

        return view('admin.classify-list', ['classify' => $classify]);
    }

    public function webSiteInfo(Request $request)
    {
        if (Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        /** @var SysInfo $sysInfo */
        $allSysInfo = SysInfo::all();

        $webInfoArr = null;
        if (count($allSysInfo) > 0) {
            foreach ($allSysInfo as $sysInfo) {
                switch ($sysInfo->type) {
                    case 'title':
                        $webInfoArr['title'] = $sysInfo->content;
                        break;
                    case 'logo':
                        $webInfoArr['logo_url'] = FileManager::getImgUrl((int)$sysInfo->content);
                        break;
                    case 'url':
                        $webInfoArr['url'] = $sysInfo->content;
                        break;
                    case 'footer':
                        $webInfoArr['footer_info'] = $sysInfo->content;
                        break;
                }
            }
        }

        return view('admin.website-info', ['webInfoArr' => $webInfoArr]);
    }

    public function footerMenu(Request $request)
    {
        if (Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        /** @var FooterMenu $footerMenu */
        $allFooterMenu = FooterMenu::wherePid(0)->get();
        if (count($allFooterMenu) > 0) {
            foreach ($allFooterMenu as $footerMenu) {
                $child = FooterMenu::wherePid($footerMenu->id)->get();
                $footerMenu->child = $child;
            }
        }

        return view('admin.footer-info', ['allFooterMenu' => $allFooterMenu]);
    }

    public function applicationFieldList(Request $request){
        if (Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $type = 'application';

        /** @var ApplicationField $applicationField */
        $applicationFieldList = ApplicationField::orderBy('created_at','desc')->get();
        if(count($applicationFieldList) > 0){
            foreach($applicationFieldList as $applicationField){
                $applicationField->thumb_img_url = FileManager::getImgUrl($applicationField->thumb_img_id);
                $applicationField->img_url       = FileManager::getImgUrl($applicationField->img_id);
            }
        }

        return view('admin.application-field-list', ['type' => $type,'applicationFieldList' => $applicationFieldList]);
    }

    public function applicationFieldSet(Request $request){
        if (Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $type = 'application';

        $id = !is_null($request->get('id')) ? $request->get('id') : 0;

        $applicationField = ApplicationField::whereId($id)->first();

        return view('admin.application-field-set',['type' => $type,'applicationField' => $applicationField]);
    }

    public function enApplicationFieldList(Request $request){
        if (Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $type = 'en_application';

        /** @var EnApplicationField $applicationField */
        $applicationFieldList = EnApplicationField::orderBy('created_at','desc')->get();
        if(count($applicationFieldList) > 0){
            foreach($applicationFieldList as $applicationField){
                $applicationField->thumb_img_url = FileManager::getImgUrl($applicationField->thumb_img_id);
                $applicationField->img_url       = FileManager::getImgUrl($applicationField->img_id);
            }
        }

        return view('admin.application-field-list', ['type' => $type,'applicationFieldList' => $applicationFieldList]);
    }

    public function enApplicationFieldSet(Request $request){
        if (Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $type = 'en_application';

        $id = !is_null($request->get('id')) ? $request->get('id') : 0;

        $applicationField = EnApplicationField::whereId($id)->first();

        return view('admin.application-field-set',['type' => $type,'applicationField' => $applicationField]);
    }

    public function goodsList(Request $request)
    {
        if (Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $type = 'product';

        /** @var GoodsProduct $goodsProduct */
        $goodsProductList = GoodsProduct::orderBy('sort', 'asc')->orderBy('created_at', 'desc')->paginate(20);
        if (count($goodsProductList) > 0) {
            foreach ($goodsProductList as $goodsProduct) {
                $goodsProduct->thumb_url = FileManager::getImgUrl($goodsProduct->thumb_id);

                /** @var Classify $thisClassify */
                $thisClassify = Classify::whereId($goodsProduct->classify_id)->first();
                if ($thisClassify) {
                    $goodsProduct->classify_name = $thisClassify->name;
                } else {
                    $goodsProduct->classify_name = null;
                }
            }
        }

        return view('admin.goods-list', ['goodsProductList' => $goodsProductList,'type' => $type]);
    }

    public function goodsSet(Request $request)
    {
        if (Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $type = 'product';

        $id = !is_null($request->get('id')) ? $request->get('id') : 0;

        $sysGoods = GoodsProduct::whereId($id)->first();

        $classifies = Classify::whereType($type)->get();

        return view('admin.goods-set', ['sysGoods' => $sysGoods, 'classifies' => $classifies,'type' => $type]);
    }

    public function goodsClassifyList(Request $request)
    {
        if (Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $type = 'product';

        $allClassifies = Classify::whereType($type)->orderBy('sort', 'asc')->orderBy('created_at', 'desc')->get();

        return view('admin.all-classify-list', ['type' => $type, 'allClassifies' => $allClassifies]);
    }

    public function newsList()
    {
        if (Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $type = 'news';

        /** @var News $news */
        $newsList = News::orderBy('created_at', 'desc')->paginate(20);
        if (count($newsList) > 0) {
            foreach ($newsList as $news) {
                $news->thumb_img_url = FileManager::getImgUrl($news->thumb_img_id);

                /** @var Classify $thisClassify */
                $thisClassify = Classify::whereId($news->classify_id)->first();
                if ($thisClassify){
                    $news->classify_name = $thisClassify->name;
                }else{
                    $news->classify_name = null;
                }
            }
        }

        return view('admin.news-list',['newsList' => $newsList,'type' => $type]);
    }

    public function newsSet(Request $request){
        if (Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $type = 'news';

        $id = !is_null($request->get('id')) ? $request->get('id') : 0;

        $thisNews = News::whereId($id)->first();

        $classifies = Classify::whereType($type)->get();

        return view('admin.news-set',['thisNews' => $thisNews,'classifies' => $classifies,'type' => $type]);
    }

    public function newsClassifyList(Request $request){
        if (Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $type = 'news';

        $allClassifies = Classify::whereType($type)->orderBy('sort', 'asc')->orderBy('created_at', 'desc')->get();

        return view('admin.all-classify-list', ['type' => $type, 'allClassifies' => $allClassifies]);
    }

    public function aboutUsList(Request $request){
        if (Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $type = 'about_us';

        /** @var AboutUs $aboutUs */
        $aboutUsList = AboutUs::orderBy('created_at','desc')->get();
        if(count($aboutUsList) > 0){
            foreach($aboutUsList as $aboutUs){
                /** @var Classify $thisClassify */
                $thisClassify = Classify::whereId($aboutUs->classify_id)->first();
                if($thisClassify){
                    $aboutUs->classify_name = $thisClassify->name;
                }else{
                    $aboutUs->classify_name = null;
                }
            }
        }

        return view('admin.about-us-list',['aboutUsList' => $aboutUsList,'type' => $type]);
    }

    public function aboutUsSet(Request $request){
        if (Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $type = 'about_us';

        $id = !is_null($request->get('id')) ? $request->get('id') : 0;

        $thisAboutUs = AboutUs::whereId($id)->first();

        $classifies = Classify::whereType('about_us')->get();

        return view('admin.about-us-set',['type' => $type,'thisAboutUs' => $thisAboutUs,'classifies' => $classifies]);
    }

    public function aboutUsClassifyList(Request $request){
        if (Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $type = 'about_us';

        $allClassifies = Classify::whereType($type)->orderBy('sort', 'asc')->orderBy('created_at', 'desc')->get();

        return view('admin.all-classify-list', ['type' => $type, 'allClassifies' => $allClassifies]);
    }

    public function humanResList(Request $request){
        if (Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $type = 'join';

        /** @var HumanResources $HumanRes */
        $HumanResList = HumanResources::orderBy('created_at','desc')->get();
        if(count($HumanResList) > 0){
            foreach($HumanResList as $HumanRes){
                /** @var Classify $thisClassify */
                $thisClassify = Classify::whereId($HumanRes->classify_id)->first();
                if($thisClassify){
                    $HumanRes->classify_name = $thisClassify->name;
                }else{
                    $HumanRes->classify_name = null;
                }
            }
        }

        return view('admin.human-res-list',['type' => $type,'HumanResList' => $HumanResList]);
    }

    public function humanResSet(Request $request){
        if (Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $type = 'join';

        $id = !is_null($request->get('id')) ? $request->get('id') : 0;

        $thisHumanRes = HumanResources::whereId($id)->first();

        $classifies = Classify::whereType('join')->get();

        return view('admin.human-res-set',['type' => $type,'thisHumanRes' => $thisHumanRes,'classifies' => $classifies]);
    }

    public function humanResClassifyList(Request $request){
        if (Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $type = 'join';

        $allClassifies = Classify::whereType($type)->orderBy('sort', 'asc')->orderBy('created_at', 'desc')->get();

        return view('admin.all-classify-list', ['type' => $type, 'allClassifies' => $allClassifies]);
    }

    public function enWebSiteInfo(Request $request){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        /** @var EnSysInfo $enSysInfo */
        $enSysInfos = EnSysInfo::all();

        $webInfoArr = null;
        if(count($enSysInfos) > 0){
            foreach($enSysInfos as $enSysInfo){
                switch ($enSysInfo->type) {
                    case 'title':
                        $webInfoArr['title'] = $enSysInfo->content;
                        break;
                    case 'logo':
                        $webInfoArr['logo_url'] = FileManager::getImgUrl((int)$enSysInfo->content);
                        break;
                    case 'url':
                        $webInfoArr['url'] = $enSysInfo->content;
                        break;
                    case 'footer':
                        $webInfoArr['footer_info'] = $enSysInfo->content;
                        break;
                }
            }
        }

        return view('admin.en-website-info',['webInfoArr' => $webInfoArr]);
    }

    public function enFooterMenu(){
        if(Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        /** @var EnFooterMenu $footerMenu */
        $allEnFooterMenu = EnFooterMenu::wherePid(0)->get();
        if (count($allEnFooterMenu) > 0) {
            foreach ($allEnFooterMenu as $footerMenu) {
                $child = EnFooterMenu::wherePid($footerMenu->id)->get();
                $footerMenu->child = $child;
            }
        }

        return view('admin.en-footer-info',['allEnFooterMenu' => $allEnFooterMenu]);
    }

    public function enGoodsList(Request $request){
        if (Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $type = 'en_product';

        /** @var EnGoodsProduct $goodsProduct */
        $goodsProductList = EnGoodsProduct::orderBy('sort', 'asc')->orderBy('created_at', 'desc')->paginate(20);
        if (count($goodsProductList) > 0) {
            foreach ($goodsProductList as $goodsProduct) {
                $goodsProduct->thumb_url = FileManager::getImgUrl($goodsProduct->thumb_id);

                /** @var Classify $thisClassify */
                $thisClassify = Classify::whereId($goodsProduct->classify_id)->first();
                if ($thisClassify) {
                    $goodsProduct->classify_name = $thisClassify->name;
                } else {
                    $goodsProduct->classify_name = null;
                }
            }
        }

        return view('admin.goods-list', ['goodsProductList' => $goodsProductList,'type' => $type]);
    }

    public function enGoodsSet(Request $request){
        if (Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $type = 'en_product';

        $id = !is_null($request->get('id')) ? $request->get('id') : 0;

        $sysGoods = EnGoodsProduct::whereId($id)->first();

        $classifies = Classify::whereType($type)->get();

        return view('admin.goods-set', ['sysGoods' => $sysGoods, 'classifies' => $classifies,'type' => $type]);
    }

    public function enGoodsClassifyList(Request $request){
        if (Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $type = 'en_product';

        $allClassifies = Classify::whereType($type)->orderBy('sort', 'asc')->orderBy('created_at', 'desc')->get();

        return view('admin.all-classify-list', ['type' => $type, 'allClassifies' => $allClassifies]);
    }

    public function enNewsList()
    {
        if (Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $type = 'en_news';

        /** @var EnNews $news */
        $newsList = EnNews::orderBy('created_at', 'desc')->paginate(20);
        if (count($newsList) > 0) {
            foreach ($newsList as $news) {
                $news->thumb_img_url = FileManager::getImgUrl($news->thumb_img_id);

                /** @var Classify $thisClassify */
                $thisClassify = Classify::whereId($news->classify_id)->first();
                if ($thisClassify){
                    $news->classify_name = $thisClassify->name;
                }else{
                    $news->classify_name = null;
                }
            }
        }

        return view('admin.news-list',['newsList' => $newsList,'type' => $type]);
    }

    public function enNewsSet(Request $request){
        if (Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $type = 'en_news';

        $id = !is_null($request->get('id')) ? $request->get('id') : 0;

        $thisNews = EnNews::whereId($id)->first();

        $classifies = Classify::whereType($type)->get();

        return view('admin.news-set',['thisNews' => $thisNews,'classifies' => $classifies,'type' => $type]);
    }

    public function enNewsClassifyList(Request $request){
        if (Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $type = 'en_news';

        $allClassifies = Classify::whereType($type)->orderBy('sort', 'asc')->orderBy('created_at', 'desc')->get();

        return view('admin.all-classify-list', ['type' => $type, 'allClassifies' => $allClassifies]);
    }

    public function enAboutUsList(Request $request){
        if (Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $type = 'en_about_us';

        /** @var EnAboutUs $aboutUs */
        $aboutUsList = EnAboutUs::orderBy('created_at','desc')->get();
        if(count($aboutUsList) > 0){
            foreach($aboutUsList as $aboutUs){
                /** @var Classify $thisClassify */
                $thisClassify = Classify::whereId($aboutUs->classify_id)->first();
                if($thisClassify){
                    $aboutUs->classify_name = $thisClassify->name;
                }else{
                    $aboutUs->classify_name = null;
                }
            }
        }

        return view('admin.about-us-list',['aboutUsList' => $aboutUsList,'type' => $type]);
    }

    public function enAboutUsSet(Request $request){
        if (Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $type = 'en_about_us';

        $id = !is_null($request->get('id')) ? $request->get('id') : 0;

        $thisAboutUs = EnAboutUs::whereId($id)->first();

        $classifies = Classify::whereType($type)->get();

        return view('admin.about-us-set',['type' => $type,'thisAboutUs' => $thisAboutUs,'classifies' => $classifies]);
    }

    public function enAboutUsClassifyList(Request $request){
        if (Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $type = 'en_about_us';

        $allClassifies = Classify::whereType($type)->orderBy('sort', 'asc')->orderBy('created_at', 'desc')->get();

        return view('admin.all-classify-list', ['type' => $type, 'allClassifies' => $allClassifies]);
    }

    public function enHumanResList(Request $request){
        if (Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $type = 'en_join';

        /** @var HumanResources $HumanRes */
        $HumanResList = EnHumanResources::orderBy('created_at','desc')->get();
        if(count($HumanResList) > 0){
            foreach($HumanResList as $HumanRes){
                /** @var Classify $thisClassify */
                $thisClassify = Classify::whereId($HumanRes->classify_id)->first();
                if($thisClassify){
                    $HumanRes->classify_name = $thisClassify->name;
                }else{
                    $HumanRes->classify_name = null;
                }
            }
        }

        return view('admin.human-res-list',['HumanResList' => $HumanResList,'type' => $type]);
    }

    public function enHumanResSet(Request $request){
        if (Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $type = 'en_join';

        $id = !is_null($request->get('id')) ? $request->get('id') : 0;

        $thisHumanRes = EnHumanResources::whereId($id)->first();

        $classifies = Classify::whereType($type)->get();

        return view('admin.human-res-set',['type' => $type,'thisHumanRes' => $thisHumanRes,'classifies' => $classifies]);
    }

    public function enHumanResClassifyList(Request $request){
        if (Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $type = 'en_join';

        $allClassifies = Classify::whereType($type)->orderBy('sort', 'asc')->orderBy('created_at', 'desc')->get();

        return view('admin.all-classify-list', ['type' => $type, 'allClassifies' => $allClassifies]);
    }

    public function enBannerList()
    {
        if (Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $type = 'en_banner';

        /** @var EnBanner $sysBanner */
        $sysBanners = EnBanner::orderBy('sort', 'asc')->orderBy('created_at', 'desc')->get();
        if (count($sysBanners) > 0) {
            foreach ($sysBanners as $sysBanner) {
                $sysBanner->img_url = FileManager::getImgUrl($sysBanner->img_id);
            }
        }

        return view('admin.banner-list', ['banners' => $sysBanners,'type' => $type]);
    }

    public function enBannerSet(Request $request)
    {
        if (Auth::guard('admin')->guest())
            return redirect(route('Admin.Home.login'));

        $type = 'en_banner';
        $id   = !is_null($request->get('id')) ? $request->get('id') : 0;

        /** @var EnBanner $thatBanner */
        $thatBanner = EnBanner::whereId($id)->first();
        if ($thatBanner) {
            $thatBanner->img_url = FileManager::getImgUrl($thatBanner->img_id);
        }

        return view('admin.banner-set', ['banner' => $thatBanner,'type' => $type]);
    }
}